﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using System.Text;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;
using System.Web.Script.Serialization;
using System.Net;

namespace weikeweixin.web.wechat.api.cs
{
    public class replyset
    {
        public string hostUrl = "http://" + HttpContext.Current.Request.Url.Authority;          //域名
        public string upfileurl = "http://file.api.weixin.qq.com/cgi-bin/media/upload";
        public string baiduImg = "http://api.map.baidu.com/staticimage?center={0},{1}&width=700&height=300&zoom=11";
        public string User_ID = "";
        public string WeChat_ID = "";
        public string WeChat_Type = "";
        public string WeChat_Name = "";



        w_caidan_dal caidandal = new w_caidan_dal();
        w_reply_dal replydal = new w_reply_dal();
        w_article_dal articledal = new w_article_dal();
        w_keyword_dal keyworddal = new w_keyword_dal();
        w_vlimg_dal vlimgdal = new w_vlimg_dal();
        w_vlimg_model vlimgmodel = new w_vlimg_model();
        w_images_dal imagesdal = new w_images_dal();

        common wxCommand = new common();
        JsonOperate JsonOperate = new JsonOperate();

        JavaScriptSerializer Jss = new JavaScriptSerializer();

        public replyset()
        { }

        #region 关注回复
        public string GetSubscribe(string FromUserName, string ToUserName)
        {
            string resXml = "";
            string sqlWhere = !string.IsNullOrEmpty(WeChat_ID) ? "WeChat_ID=" + WeChat_ID + " and reply_fangshi=2" : "";

            DataTable dtSubscribe = replydal.GetRandomList(sqlWhere, "1").Tables[0];

            if (dtSubscribe.Rows.Count > 0)
            {
                string article_id = dtSubscribe.Rows[0]["article_id"].ToString();
                string reply_type = dtSubscribe.Rows[0]["reply_type"].ToString();
                string reply_text = dtSubscribe.Rows[0]["reply_text"].ToString();

                if (reply_type == "text")
                {
                    resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + reply_text + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                }
                else
                {
                    resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID);
                }
            }

            return resXml;
        }
        #endregion 关注回复

        #region 默认回复
        public string GetDefault(string FromUserName, string ToUserName, string WeChat_ID, string User_ID)
        {
            string resXml = "";
            string sqlWhere = !string.IsNullOrEmpty(WeChat_ID) ? "WeChat_ID=" + WeChat_ID + " and reply_fangshi=1" : "";

            DataTable dtDefault = replydal.GetRandomList(sqlWhere, "1").Tables[0];

            if (dtDefault.Rows.Count > 0)
            {
                string article_id = dtDefault.Rows[0]["article_id"].ToString();
                string reply_type = dtDefault.Rows[0]["reply_type"].ToString();
                string reply_text = dtDefault.Rows[0]["reply_text"].ToString();

                if (reply_type == "text")
                {
                    resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + reply_text + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                }
                else
                {
                    resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID);
                }
            }

            return resXml;
        }
        #endregion 默认回复

        #region 图片回复
        public string GetImageReply(string FromUserName, string ToUserName, string UrlImg)
        {
            string resXml = "";
            string sqlWhere = !string.IsNullOrEmpty(WeChat_ID) ? "WeChat_ID=" + WeChat_ID + " and reply_fangshi=3" : "";

            DataTable dtImageReply = replydal.GetRandomList(sqlWhere, "1").Tables[0];
            DataTable dtimages = imagesdal.GetList("user_id=" + User_ID).Tables[0];
            if (dtImageReply.Rows.Count > 0)
            {
                string Article_ID = dtImageReply.Rows[0]["article_id"].ToString();
                string ImageReply_ReplyType = dtImageReply.Rows[0]["reply_type"].ToString();
                string ImageReply_ReplyText = dtImageReply.Rows[0]["reply_text"].ToString();

                if (ImageReply_ReplyType == "text")
                {
                    resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + ImageReply_ReplyText + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                }
                else if (ImageReply_ReplyType == "image")
                {
                   
                    string vimg = wechatimage.AddVImage(UrlImg,User_ID);//根据发送过来的图片给其收集返回收集后的图片相对路径

                    

                    string Title = "图片收集";
                    string Description = "";
                    string PicUrl = hostUrl + vimg;

                    vlimgmodel.mid = User_ID;
                    vlimgmodel.vl_img = PicUrl;
                    vlimgmodel.vl_title = Title;
                    vlimgmodel.vl_description = Description;
                    vlimgmodel.vl_wechat_name = WeChat_Name;
                    vlimgmodel.vl_wechat_openid = FromUserName;

                    int VimgLogID = vlimgdal.AddPro(vlimgmodel);
                    if (VimgLogID > 0)
                    {
                        resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + dtimages.Rows[0]["images_tishi"] + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                    }
                    else if (VimgLogID == -2)
                    {
                        resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + dtimages.Rows[0]["images_tishi2"] + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                    }
                    else
                    {
                        resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + "非常抱歉，亲，发生错误（" + VimgLogID + "），请重试或联系IdeaSoft软件！" + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                    }
                }
                else
                {
                    resXml = GetArticle(FromUserName, ToUserName, Article_ID, User_ID);
                }
            }

            return resXml;
        }
        #endregion 默认回复

        #region 关键字回复
        public string GetKeyword(string FromUserName, string ToUserName, string Content)
        {
            string resXml = "";
            string sqlWhere = "wechat_id=" + WeChat_ID + " and keyword_name='" + Content+"'";

            DataTable dtKeyword = keyworddal.GetList(sqlWhere).Tables[0];
            
            if (dtKeyword.Rows.Count > 0)
            {
                dtKeyword = keyworddal.GetRandomList(sqlWhere, "1").Tables[0];

                if (dtKeyword.Rows.Count > 0)
                {
                    string article_id = dtKeyword.Rows[0]["article_id"].ToString();
                    string keyword_type = dtKeyword.Rows[0]["keyword_type"].ToString();
                    string keyword_text = dtKeyword.Rows[0]["keyword_text"].ToString();

                    switch (keyword_type)
                    {
                        case "text":
                            resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + keyword_text + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                            break;
                        case "news":
                            resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID);
                            break;
                    }
                }
            }
            else
            {
                resXml = GetDefault(FromUserName, ToUserName, WeChat_ID, User_ID);
            }

            return resXml;
        }
        #endregion 关键字回复

        #region 菜单单击
        public string GetMenuClick(string FromUserName, string ToUserName, string EventKey)
        {
            string resXml = "";
            string sqlWhere = "wechat_id=" + WeChat_ID + " and caidan_key='" + EventKey + "'";

            WriteTxt(sqlWhere);
            try
            {

                DataTable dtMenu = caidandal.GetList(sqlWhere).Tables[0];

                if (dtMenu.Rows.Count > 0)
                {
                    string article_id = dtMenu.Rows[0]["article_id"].ToString();
                    string caidan_retype = dtMenu.Rows[0]["caidan_retype"].ToString();
                    string caidan_retext = dtMenu.Rows[0]["caidan_retext"].ToString();


                    switch (caidan_retype)
                    {
                        case "text":
                            resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + caidan_retext + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                            break;
                        case "news":
                            resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                WriteTxt("异常：" + ex.Message + "Struck:" + ex.StackTrace.ToString());
            }

            return resXml;
        }
        #endregion 菜单单击

        #region 获取图文列表
        public string GetArticle(string FromUserName, string ToUserName, string Article_ID, string User_ID)
        {
            string resXml = "";

            DataTable dtArticle = articledal.GetList("article_id=" + Article_ID + " OR article_layid=" + Article_ID).Tables[0];

            if (dtArticle.Rows.Count > 0)
            {
                resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>" + dtArticle.Rows.Count + "</ArticleCount><Articles>";

                foreach (DataRow Row in dtArticle.Rows)
                {
                    string article_title = Row["article_title"].ToString();
                    string article_description = Row["article_description"].ToString();
                    string article_picurl = Row["article_picurl"].ToString();
                    string article_url = Row["article_url"].ToString();
                    string article_type = Row["article_type"].ToString();

                    switch (article_type)
                    {
                        case "Content":
                            article_url = hostUrl + "/web/wechat/api/article.aspx?aid=" + Row["Article_ID"].ToString();
                            break;
                        case "Href":
                            article_url = Row["article_url"].ToString();
                            break;
                    }

                    if (string.IsNullOrEmpty(article_url))
                    {
                        article_url = hostUrl + "/web/wechat/api/article.aspx?aid=" + Row["Article_ID"].ToString();
                    }

                    article_url += (article_url.IndexOf("uid=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "uid=" + User_ID);
                    article_url += (article_url.IndexOf("wxid=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "wxid=" + FromUserName);
                    article_url += (article_url.IndexOf("wxref=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "wxref=mp.weixin.qq.com");

                    resXml += "<item><Title><![CDATA[" + article_title + "]]></Title><Description><![CDATA[" + article_description + "]]></Description><PicUrl><![CDATA[" + article_picurl + "]]></PicUrl><Url><![CDATA[" + article_url + "]]></Url></item>";
                }

                resXml += "</Articles><FuncFlag>1</FuncFlag></xml>";
            }

            return resXml;
        }
        #endregion 获取图文列表

        //#region 此为帮客户做的动态生成图文列表
        //public string GetMoveArticle(string FromUserName, string ToUserName, string Article_ID, string User_ID)
        //{
        //    string resXml = "";
        //    string respText = "";
        //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(moveurl2);
        //    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        //    using (Stream resStream = response.GetResponseStream())
        //    {
        //        StreamReader reader = new StreamReader(resStream, Encoding.Default);
        //        respText = reader.ReadToEnd();
        //        resStream.Close();
        //    }

        //    Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);


        //        resXml = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>" + respDic.Count + "</ArticleCount><Articles>";


        //        for (int i = 0;i < 9 ; i++)
        //        {
        //            string article_title = respDic["FabuTime"].ToString();
        //            string article_description = "无";
        //            string article_picurl = "http://weixin.hzqx.com/html/shzs/mllm1.jpg";
        //            string article_url = "http://weixin.hzqx.com/html/qw.aspx?&OpenId=oUsDsjro37e7F5B9cVkAQaJEP8es";

                    

        //            article_url += (article_url.IndexOf("uid=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "uid=" + User_ID);
        //            article_url += (article_url.IndexOf("wxid=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "wxid=" + FromUserName);
        //            article_url += (article_url.IndexOf("wxref=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "wxref=mp.weixin.qq.com");

        //            resXml += "<item><Title><![CDATA[" + article_title + "]]></Title><Description><![CDATA[" + article_description + "]]></Description><PicUrl><![CDATA[" + article_picurl + "]]></PicUrl><Url><![CDATA[" + article_url + "]]></Url></item>";
        //        }

        //        resXml += "</Articles><FuncFlag>1</FuncFlag></xml>";

        //    return resXml;
        //}

        //#endregion

        #region 通用方法
        /// <summary>
        /// unix时间转换为datetime
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        private DateTime UnixTimeToTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// datetime转换为unixtime
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 记录bug，以便调试
        /// </summary>
        /// <returns></returns>
        public bool WriteTxt(string str)
        {
            try
            {
                FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("Log/wxbugLog.txt"), FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.WriteLine(str);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        #endregion 通用方法
    }
}